﻿#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
#include<string.h>
using namespace std;

namespace zzx
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin() const
		{
			return _str;
		}

		const_iterator end() const
		{
			return _str + _size;
		}

		//string()
		//	:_str(new char[1]{'\0'})
		//	,_size(0)
		//	,_capacity(0)
		//{}

		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}

		string(const char* str = "")
			:_size(strlen(str))
		{
			_capacity = _size;
			_str = new char[_size + 1];
			strcpy(_str, str);
		}
		string(size_t n, char ch)
			:_str(new char[n + 1])
			,_size(n)
			,_capacity(n)
		{
			for (size_t i = 0; i < n; i++)
			{
				_str[i] = ch;
			}
			_str[_size] = '\0';
		}

		// s2(s1)
		string(const string& s)
		{
			_str = new char[s._capacity + 1];
			strcpy(_str, s._str);
			_size = s._size;
			_capacity = s._capacity;
		}

		string& operator=(string s)
		{
			swap(s);
			return *this;
		}
		//string& operator=(const string& s);

		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = 0;
			_capacity = 0;
		}

		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}

		const char* c_str() const
		{
			return _str;
		}

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				cout << "reserve:" << n << endl;
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}
		void push_back(char ch)
		{
			if (_size + 1 > _capacity)
			{
				// 扩容
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}

			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}
		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				// 扩容
				size_t newCapacity = 2 * _capacity;
				if (_size + len > 2 * _capacity)
				{
					newCapacity = _size + len;
				}
				reserve(newCapacity);
			}
		}
		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}
		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}
		string operator+(const string& other) const {
			size_t total_len = _size + other._size;

			// 情况1：当前缓冲区足够容纳拼接结果
			if (_capacity >= total_len) {
				// 复用当前对象的内存（避免 new）
				string result(*this); // 拷贝当前对象（深拷贝）
				memcpy(result._str + _size, other._str, other._size); // 追加其他内容
				result._size = total_len; // 更新长度
				return result;
			}
			// 情况2：缓冲区不足，分配新内存（仅一次 new）
			else {
				char* new_str = new char[total_len + 1]; // 多分配1字节给'\0'
				memcpy(new_str, _str, _size);           // 复制原内容
				memcpy(new_str + _size, other._str, other._size); // 复制其他内容
				new_str[total_len] = '\0'; // 显式终止符

				return string(new_str, total_len, total_len); // 通过辅助构造函数接管内存
			}
		}
		string(char* str, size_t size, size_t capacity)
			: _str(str), _size(size), _capacity(capacity) {}
		void insert(size_t pos, size_t n, char ch)
		{
			assert(pos <= _size);
			assert(n > 0);

			if (_size + n > _capacity)
			{
				// 扩容
				size_t newCapacity = 2 * _capacity;
				if (_size + n > 2 * _capacity)
				{
					newCapacity = _size + n;
				}

				reserve(newCapacity);
			}
			// 挪动数据
		/*	int end = _size;
			while (end >= (int)pos)
			{
				_str[end + n] = _str[end];
				--end;
			}*/
			size_t end = _size + n;
			while (end > pos + n - 1)
			{
				_str[end] = _str[end - n];
				--end;
			}

			for (size_t i = 0; i < n; i++)
			{
				_str[pos + i] = ch;
			}
			_size += n;
		}
		void insert(size_t pos, const char* str)
		{
			size_t n = strlen(str);
			//insert(pos, n, 'x');
			// 覆盖一下
			for (size_t i = 0; i < n; i++)
			{
				_str[pos + i] = str[i];
			}
		}
		void erase(size_t pos = 0, size_t len = npos)
		{
			if (len >= _size - pos)
			{
				// 删完了
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				size_t end = pos + len;
				while (end <= _size)
				{
					_str[end - len] = _str[end];
					++end;
				}
				_size -= len;
			}
		}

		size_t find(char ch, size_t pos = 0)
		{
			for (size_t i = pos; i < _size; i++)
			{
				if (_str[i] == ch)
				{
					return i;
				}
			}

			return npos;
		}
		size_t find(const char* str, size_t pos = 0)
		{
			const char* p = strstr(_str + pos, str);
			if (p == nullptr)
			{
				return npos;
			}
			else
			{
				return p - _str;
			}
		}

		size_t size() const
		{
			return _size;
		}

		size_t capacity() const
		{
			return _size;
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			assert(pos < _size);
			return _str[pos];
		}

		string substr(size_t pos, size_t len = npos)
		{
			size_t leftlen = _size - pos;
			if (len > leftlen)
				len = leftlen;

			string tmp;
			tmp.reserve(len);
			for (size_t i = 0; i < len; i++)
			{
				tmp += _str[pos + i];
			}

			return tmp;
		}

		bool operator==(const string& s) const
		{
			return strcmp(_str, s._str) == 0;
		}
		bool operator!=(const string& s) const
		{
			return !(*this == s);
		}
		bool operator<(const string& s) const
		{
			return strcmp(_str, s._str) < 0;
		}
		bool operator<=(const string& s) const
		{
			return *this < s || *this == s;
		}
		bool operator>(const string& s) const
		{
			return !(*this <= s);
		}
		bool operator>=(const string& s) const
		{
			return !(*this < s);
		}
	private:
		// 
		char* _str = nullptr;
		size_t _size = 0;
		size_t _capacity = 0;

		const static size_t npos = -1;
	};

	// cout<<s1
	ostream& operator<<(ostream& out, const string& s)
	{
		for (auto ch : s)
		{
			out << ch;
		}

		return out;
	}
	// cin>>s1
	istream& operator>>(istream& in, string& s)
	{
		s.clear();

		// 输入短串，不会浪费空间
		// 输入长串，避免不断扩容
		const size_t N = 1024;
		char buff[N];
		int i = 0;
		char ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == N - 1)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}

			ch = in.get();
		}

		if (i > 0)
		{
			buff[i] = '\0';
			s += buff;
		}

		return in;
	}

	istream& getline(istream& is, string& s, char delim = '\n')
	{
		s.clear();

		const size_t N = 1024;
		char buff[N];
		int i = 0;
		char ch = is.get();
		while (ch != delim)
		{
			buff[i++] = ch;
			if (i == N - 1)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}

			ch = is.get();
		}

		if (i > 0)
		{
			buff[i] = '\0';
			s += buff;
		}

		return is;
	}

	void test_string1()
	{
		string s1 = "hello yidaoyun!";
		cout << s1.c_str() << endl;

		string s2;
		cout << s2.c_str() << endl;

		s1 += 'z';
		cout << s1.c_str() << endl;

	}
	void test_string2()
	{
		string s1 = "123456789";
		string s2 = "abc";
		string s3 = s1 + s2;
		cout << s3.c_str() << endl;
	}
	//void test_string3();
	//void test_string4();
	//void test_string5();
	//void test_string6();
	//void test_string7();
}
